package leetcode._05_二叉树;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author pppppp
 * @date 2022/3/30 16:08
 */
public class _106_从中序与后序遍历序列构造二叉树 {

    @Test
    public void T_0() {
        int[][] inorder = {{9, 3, 15, 20, 7}, {1, 2, 3, 4}, {1, 2, 3, 4}};
        int[][] postorder = {{9, 15, 7, 20, 3}, {3, 2, 4, 1}, {4, 3, 2, 1}};

        // int [] ans = {3,9,20,null,null,15,7};
        for (int i = 0; i < inorder.length; i++) {
            // TreeNode node = buildTree_0(inorder[i], postorder[i]);
            TreeNode node = buildTree_1(inorder[i], postorder[i]);
            System.out.println();
        }

    }

    /*1.gf*/
    int post_idx;
    int[] postorder;
    int[] inorder;
    Map<Integer, Integer> idx_map = new HashMap<Integer, Integer>();

    public TreeNode buildTree_1(int[] inorder, int[] postorder) {
        this.postorder = postorder;
        this.inorder = inorder;
        // 从后序遍历的最后一个元素开始
        post_idx = postorder.length - 1;

        // 建立（元素，下标）键值对的哈希表
        int idx = 0;
        for (Integer val : inorder) {
            idx_map.put(val, idx++);
        }

        return helper(0, inorder.length - 1);
    }
    public TreeNode helper(int in_left, int in_right) {
        // 如果这里没有节点构造二叉树了，就结束
        if (in_left > in_right) {
            return null;
        }

        // 选择 post_idx 位置的元素作为当前子树根节点
        int root_val = postorder[post_idx];
        TreeNode root = new TreeNode(root_val);

        // 根据 root 所在位置分成左右两棵子树
        int index = idx_map.get(root_val);

        // 下标减一
        post_idx--;
        // 构造右子树
        root.right = helper(index + 1, in_right);
        // 构造左子树
        root.left = helper(in_left, index - 1);
        return root;
    }




    /*0.数组起始，多条件判断*/
    public TreeNode buildTree_0(int[] inorder, int[] postorder) {
        HashMap<Integer, Integer> imap = new HashMap<>();
        for (int i = 0; i < inorder.length; i++) {
            imap.put(inorder[i], i);
        }
        return buildTreeAssit(inorder, 0, inorder.length, postorder, 0, imap);
    }

    private TreeNode buildTreeAssit(int[] inorder, int is, int len,
                                    int[] postorder, int ps,
                                    HashMap<Integer, Integer> imap) {
        if (len <= 0) {
            return null;
        }
        if (len == 1) {
            return new TreeNode(inorder[is]);
        }
        TreeNode root = new TreeNode(postorder[ps + len - 1]);
        Integer iIndex = imap.get(postorder[ps + len - 1]);

        int lenL = iIndex - is;
        if (lenL > 0) {
            root.left = buildTreeAssit(inorder, is, lenL, postorder, ps, imap);
        } else {
            lenL = 0;
        }
        int lenR = len - lenL - 1;
        if (lenR > 0) {
            root.right = buildTreeAssit(inorder, iIndex + 1, lenR, postorder, ps+lenL, imap);
        }
        return root;
    }


}
